'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _toConsumableArray2 = _interopRequireDefault(
  require('@babel/runtime/helpers/toConsumableArray'),
);
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _antd = require('antd');
var _createField = _interopRequireDefault(require('../../../createField'));
var _excluded = ['children'],
  _excluded2 = ['children', 'dataSource', 'targetKeys', 'treeProps'];
var generateTree = function generateTree() {
  var treeNodes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var checkedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  return treeNodes.map(function (_ref) {
    var children = _ref.children,
      props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
    return (0, _objectSpread2.default)(
      (0, _objectSpread2.default)({}, props),
      {},
      {
        disabled: checkedKeys.includes(props.key),
        children: generateTree(children, checkedKeys),
      },
    );
  });
};
var TableTransfer = function TableTransfer(_ref2) {
  var children = _ref2.children,
    dataSource = _ref2.dataSource,
    _ref2$targetKeys = _ref2.targetKeys,
    targetKeys = _ref2$targetKeys === void 0 ? [] : _ref2$targetKeys,
    _ref2$treeProps = _ref2.treeProps,
    treeProps = _ref2$treeProps === void 0 ? {} : _ref2$treeProps,
    rest = (0, _objectWithoutProperties2.default)(_ref2, _excluded2);
  var transferDataSource = (0, _react.useMemo)(
    function () {
      var result = [];
      function flatten() {
        var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
        list.forEach(function (item) {
          result.push(item);
          if (item.children) flatten(item.children);
        });
      }
      flatten(dataSource);
      return result;
    },
    [dataSource],
  );
  return /*#__PURE__*/ _react.default.createElement(
    _antd.Transfer,
    (0, _objectSpread2.default)(
      (0, _objectSpread2.default)({}, rest),
      {},
      {
        targetKeys: targetKeys,
        dataSource: transferDataSource,
      },
    ),
    function (_ref3) {
      var direction = _ref3.direction,
        onItemSelect = _ref3.onItemSelect,
        selectedKeys = _ref3.selectedKeys;
      if (direction === 'left') {
        var checkedKeys = [].concat(
          (0, _toConsumableArray2.default)(selectedKeys),
          (0, _toConsumableArray2.default)(targetKeys),
        );
        return /*#__PURE__*/ _react.default.createElement(
          _antd.Tree,
          (0, _objectSpread2.default)(
            (0, _objectSpread2.default)(
              {
                blockNode: true,
                checkable: true,
                checkStrictly: true,
                defaultExpandAll: true,
              },
              treeProps,
            ),
            {},
            {
              checkedKeys: checkedKeys,
              treeData: generateTree(dataSource, targetKeys),
              onCheck: function onCheck(_, _ref4) {
                var key = _ref4.node.key;
                onItemSelect(key, !checkedKeys.includes(key));
              },
              onSelect: function onSelect(_, _ref5) {
                var key = _ref5.node.key;
                onItemSelect(key, !checkedKeys.includes(key));
              },
            },
          ),
        );
      }
    },
  );
};
var _default = (0, _createField.default)(TableTransfer, {
  defaultFormItemProps: {
    valuePropName: 'targetKeys',
  },
});
exports.default = _default;
